<template>
  <div class="px-4 h-full overflow-hidden">
    <div class="flex justify-between items-center py-2">
      <a-input-search v-model:value="searchKey" class="w-64" placeholder="姓名/助记码/住院号/床位号" @keyup.enter="getData" @search="getData" />
      <!-- <a-button type="primary" @click="open = true">导出</a-button> -->
      <BaseExport type="住院" name="退院" code="001063" :search-params="{ status: 31, bqbh: store.areaCode, pyjm: searchKey }">导出</BaseExport>
    </div>
    <BaseTable column-code="001063" :loading="loading" :scroll="{ x: 2300 }" :data-source="tableData" fill>
      <template #bodyCell="{ column, index, record }">
        <template v-if="column.dataIndex === 'xh'">
          {{ index + 1 }}
        </template>
        <template v-if="column.dataIndex === 'tyStatus'">
          <a-tag v-if="record.tyStatus === 1" color="green">护士确认</a-tag>
          <a-tag v-else-if="record.tyStatus === 2" color="red">已退院</a-tag>
          <a-tag v-else color="blue">医生确认</a-tag>
        </template>
        <template v-if="column.dataIndex === 'lastBedCode'">
          {{ record.inpTransferRecord?.originalBedCode || record.lastBedCode }}
        </template>
        <template v-if="column.dataIndex === 'updateUserName'">
          {{ record.inpTransferZone?.createUserName }}
        </template>
        <template v-if="column.dataIndex === 'createTime'">
          {{ record.inpTransferZone?.createTime }}
        </template>
        <template v-if="column.dataIndex === 'brName'">
          <InPatientPopover
            v-model:open="record.openPopover"
            is-modify
            :patient-info="record"
            @change="getData"
            @click="record.openPopover = !record.openPopover"
          >
            <div class="cursor-pointer text-primary hover:underline">{{ record.brName }}</div>
          </InPatientPopover>
        </template>
        <template v-if="column.dataIndex === 'action'">
          <BaseButton type="link" size="small" :disabled="record.tyStatus !== 1" @click="onActionCancel(record)"> 退院 </BaseButton>
        </template>
      </template>
    </BaseTable>
    <!-- 退费弹窗 -->
    <ReturnChargeModal v-model:open="returnChargeModel" :register-price="rowData.dpJe" @success="onReturnSuccessPrice" />
  </div>
</template>
<script setup>
import { ref } from 'vue'
import { message } from 'ant-design-vue'
import BaseTable from '@/components/BaseTable'
import ReturnChargeModal from '@/components/ChargeModal/return'
// import useStore from '../../store'

const store = {}
const returnChargeModel = ref(false)
const rowData = ref({})
const searchKey = ref('')
const { loading, tableData, getData } = useTable({
  url: '/inp/zydj/getInpRecordList',
  immediate: true,
  pagination: false,
  formatParams: () => {
    return {
      status: 31,
      bqbh: '',
      pyjm: searchKey.value
    }
  }
})
//清床
const onClearBed = async () => {
  const { code } = await post('/inp/zyhs/clearBed', {
    bedCode: rowData.value.lastBedCode,
    inplsh: rowData.value.inplsh,
    wardId: rowData.value.lastWardId
  })
  if (code !== 0) return message.error('清床失败')
  message.success('清床成功')
  getData()
}
//申请退院
const onNurseReturn = async () => {
  const { code, msg } = await post('/inp/zyys/doApplyInpRevocation', {
    inplsh: rowData.value.inplsh,
    cancelMemo: '',
    status: 2
  })
  if (code !== 0) return message.error(msg || '操作失败')
  message.success('操作成功')
  getData()
}
const onActionCancel = (row) => {
  rowData.value = row
  returnChargeModel.value = true
}
const onReturnSuccessPrice = async (params) => {
  const res = await post('/inp/inpComprehensive/doReturnCharge', {
    typeOperation: '1',
    inplsh: rowData.value.inplsh,
    ...params
  })
  if (res.code !== 0) {
    message.error(res.msg || '操作失败')
    return
  } else {
    message.success(res.msg)
    returnChargeModel.value = false
    onNurseReturn()
    onClearBed()
  }
}
</script>
